package com.example.final_project.Cate.frag;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Cate.adapter.SignAdapter;
import com.example.final_project.Cate.entry.CarSign;
import com.example.final_project.R;
import com.gjiazhe.wavesidebar.WaveSideBar;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

public class CateFragment extends Fragment {

    private Context context;
    private RecyclerView recyclerView;
    public static final int LOADING=0;
    private View view;
    private List<CarSign> cars;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_cate, container, false);
        context = getContext();
        RefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(context));
        refreshLayout.setRefreshFooter(new ClassicsFooter(context));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
        initData();
        WaveSideBar sideBar = view.findViewById(R.id.side_bar);
        sideBar.setIndexItems("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#");
        sideBar.setOnSelectIndexItemListener(new WaveSideBar.OnSelectIndexItemListener() {
            @Override
            public void onSelectIndexItem(String index) {
                for (int i=0;i<cars.size();i++){
                    if(index.equals(cars.get(i).getIndex())){
                        recyclerView.scrollToPosition(i);
                    }
                }
            }
        });

        return view;
    }

    private Handler handler = new Handler(Looper.getMainLooper()){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case LOADING:
                    cars = (List<CarSign>)msg.obj;
                    recyclerView = view.findViewById(R.id.cate_sign);
                    SignAdapter signAdapter = new SignAdapter(context, cars);
                    recyclerView.setAdapter(signAdapter);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,RecyclerView.VERTICAL,false);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    break;
            }

        }
    };

    public void initData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/getscar")
                .bind(context)
                .setOnResList(CarSign.class,cars ->{
                    Message message = Message.obtain();
                    message.what = LOADING;
                    message.obj = cars;
                    handler.sendMessage(message);
                })
                .get();
    }

}